﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using Common.ControlDevice;

namespace Common.Tool
{
    public class ResultHandles
    {
        ListView ResultListView = null;

        //信息提示窗口
        private ObservableCollection<ErrorInfo> resultCollection = new ObservableCollection<ErrorInfo>();
        public ObservableCollection<ErrorInfo> ResultCollection
        {
            get { return resultCollection; }
            set
            {
                resultCollection = value;
                OnNotifyPropertyChanged("ResultCollection");
            }
        }

        private ObservableCollection<ErrorInfo> observableCollection = new ObservableCollection<ErrorInfo>();
        public ObservableCollection<ErrorInfo> ObservableCollection
        {
            get { return observableCollection; }
            set
            {
                observableCollection = value;
                OnNotifyPropertyChanged("ObservableCollection");
            }
        }

        private bool showWarning = true;
        public bool ShowWarning
        {
            get { return showWarning; }
            set
            {
                if (value != showWarning)
                {
                    showWarning = value;
                    UpdateItems();
                }
            }
        }
        //2018.09.04 he 增：显示错误
        private bool showError = true;
        public bool ShowError
        {
            get { return showError; }
            set
            {
                if (value != showError)
                {
                    showError = value;
                    UpdateItems();
                }
            }
        }

        private bool showInfo = true;
        public bool ShowInfo
        {
            get { return showInfo; }
            set
            {
                if (value != showInfo)
                {
                    showInfo = value;
                    UpdateItems();
                }
            }
        }
      
        /**
         * \brief 属性更改事件触发处理
         * 
         *  \param [in] PropertyName 属性名
         *  \retval void
         */
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnNotifyPropertyChanged(string propertyName)
        {
            if (null != PropertyChanged)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public void SetResultHandles(ListView ResultListView)
        {
            this.ResultListView = ResultListView;        
            this.ResultListView.ItemsSource = ObservableCollection;
        }

        public void UpdateItems()
        {
            observableCollection.Clear();
            foreach (var item in ResultCollection)
            {
                //根据勾选的显示类型，过滤显示的内容;info类型的数据，只导出，不在界面显示
                if ((ShowError && ErrorLevelCategory.Error == item.ErrorLevel) || (ShowWarning && ErrorLevelCategory.Warning == item.ErrorLevel)
                    || (ShowInfo && ErrorLevelCategory.Info == item.ErrorLevel))
                {
                    observableCollection.Add(item);
                }
            }
        }

        //2021.01.28 wsy 增：用于处理当对象更改时，将滚动条滚动到最后
        public void Items_CollectionChanged()
        {
            UpdateItems();
            if (ResultListView.Items.Count > 0)
            {
                try
                {
                    ResultListView.ScrollIntoView(ResultListView.Items[ResultListView.Items.Count - 1]);
                }
                catch (System.Exception)
                {
                    ResultListView.Items.Refresh();
                    Items_CollectionChanged();
                }
            }
        }
    }
}
